package com.hontech.bv.ui.window

import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import com.hontech.bv.R
import com.hontech.bv.task.onClick
import com.hontech.bv.ui.AutoHideKeyActivity
import com.hontech.bv.utils.dimenIntById

private val width = dimenIntById(R.dimen.w600)

private fun createButton(text: String, root: ViewGroup): Button {
    val view = LayoutInflater.from(root.context).inflate(R.layout.btn_line, root, false)
    val btn = view.findViewById<Button>(R.id.id_btn_btn)
    btn.text = text
    return btn
}

class ButtonKv(val text: String, val cb: suspend () -> Unit)

class ButtonDialog(title: String, btns: Array<ButtonKv>) : MoveableWindow(R.layout.dialog_n_button, width, ViewGroup.LayoutParams.WRAP_CONTENT, true) {

    private val btnClose = view.findViewById<Button>(R.id.id_nbutton_btn_close)
    private val tvTitle = view.findViewById<TextView>(R.id.id_nbutton_tv_title)

    init {
        btnClose.setOnClickListener { dismiss() }
        tvTitle.text = title

        val layout = view.findViewById<LinearLayout>(R.id.id_nbutton_linear)

        for (kv in btns) {
            val btn = createButton(kv.text, layout)
            layout.addView(btn)
            btn.onClick(kv.cb)
        }
    }

    fun show() {
        window.showAtLocation(AutoHideKeyActivity.parentView(), Gravity.CENTER, 0, 0)
    }
}




